﻿using AbpToDomain.EntityInfo;
using AbpToDomain.View;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata;
using Microsoft.Identity.Client;
using Volo.Abp.Data;
using Volo.Abp.EntityFrameworkCore;

namespace AbpEntityFrameworkcore.DbContext
{
    [ConnectionStringName("Lmsconnstr")]
    public class MyDbContext : AbpDbContext<MyDbContext>
    {

        public DbSet<Box> boxs { get; set; }
        public DbSet<MaterialBase> materialBases { get; set; }
        public DbSet<MaterialOrder> materialOrders { get; set; }
        public DbSet<Warehouse> warehouses { get; set; }
        public DbSet<BoxGrid> boxGrids { get; set; }
        public DbSet<WarehouseLine> warehouseLines { get; set; }
        public DbSet<WorkerOrder> workerOrders { get; set; }
        public DbSet<NeetOutMaterial> neetOutMaterials { get; set; }
        public DbSet<Inlethouse> inlethouses { get; set; }
        public DbSet<InlethouseLine> inlethousesLine { get; set; }
        public DbSet<TaskOrder> taskOrders { get; set; }
        public DbSet<lognote> lognotes { get; set; }
        public DbSet<PutInOrder> putInOrder { get; set; }
        public DbSet<PutOutOrder> putOutOrder { get; set; }
        public DbSet<BoxPosition> BoxPositions { get; set; }
        public DbSet<Unusual> Unusuals { get; set; }
        public DbSet<BoxType> BoxTypes { get; set; }

        public DbSet<MaterialProperty> materialProperties { get; set; }
        public virtual DbSet<View_Box> viewBox { get; set; }
        public virtual DbSet<View_Material> View_Materials { get; set; }
        public virtual DbSet<View_TaskOrder> View_TaskOrders { get; set; }
        public virtual DbSet<View_Box_WarehouseLine> View_Box_WarehouseLines { get; set; }
        public virtual DbSet<View_Material_WarehouseLine> View_Material_WarehouseLines { get; set; }
        public virtual DbSet<View_WarehouseLine> View_WarehouseLines { get; set; }
                          
        public virtual DbSet<View_NeetOutMaterial> View_NeetOutMaterials { get; set; }
        



        public MyDbContext(DbContextOptions<MyDbContext> options) : base(options)
        {

      
        }
     

     

        protected override void OnModelCreating(ModelBuilder builder)
        {
            base.OnModelCreating(builder);
            builder.Entity<BoxPosition>(n =>
            {
                n.ToTable("BoxPosition");
                n.HasOne<BoxGrid>(c => c.TheBoxGrid).WithMany(a => a.BoxPositions).IsRequired();
                n.HasOne<MaterialBase>(o=>o.MaterialBase).WithOne(d=>d.BoxPosition).HasForeignKey<MaterialBase>(o=>o.Id_BoxPosition);
            });
            builder.Entity<Warehouse>(n =>
            {
                n.ToTable("Warehouse");       
            });
            builder.Entity<PutInOrder>(n =>
            {
                n.ToTable("PutInOrder");
            });
            builder.Entity<PutOutOrder>(n =>
            {
                n.ToTable("PutOutOrder");
            });
            builder.Entity<Box>(n =>
            {
                n.ToTable("Box");
                n.HasOne<TaskOrder>(o => o.TaskOrder).
                WithOne(d => d.Box).
                HasForeignKey<TaskOrder>(o => o.Id_Box);
                n.HasOne<BoxType>(o=>o.BoxType).WithMany(a=>a.Boxes).
                IsRequired().OnDelete(DeleteBehavior.NoAction);

            });
            builder.Entity<BoxType>(n =>
            {
                n.ToTable("BoxType");
            });
            builder.Entity<MaterialBase>(n =>
            {
                n.ToTable("MaterialBase");           
            });
            builder.Entity<Unusual>(n =>
            {
                n.ToTable("Unusual");
            });
            builder.Entity<MaterialOrder>(n =>
            {
                n.ToTable("MaterialOrder");
             
                n.HasMany<MaterialBase>(s => s.materialBases).WithMany(t => t.MaterialOrders).
                UsingEntity(j => j.ToTable("T_MaterialBases_MaterialOrders"));

               
            });
            builder.Entity<BoxGrid>(n =>
            {
                n.ToTable("BoxGrid");
                n.HasOne<Box>(c=>c.TheBox).WithMany(a=>a.BoxGrids).IsRequired();

            });
            builder.Entity<WarehouseLine>(n =>
            {
                n.ToTable("WarehouseLine");
                n.HasOne<Warehouse>(c => c.TheWarehouse).WithMany(a => a.warehouselines).IsRequired();
                n.HasOne<Box>(o => o.Box).WithOne(d => d.WarehouseLine).HasForeignKey<Box>(o => o.Id_WarehouseLine);
                n.HasOne<BoxType>(c=>c.BoxType).WithMany(a=>a.WarehouseLines).IsRequired().OnDelete(DeleteBehavior.NoAction);
            
            });
            builder.Entity<WorkerOrder>(n =>
            {
                n.ToTable("WorkerOrder");
                n.HasOne<MaterialOrder>(c=>c.TheMaterialOreder).WithMany(a=>a.workerOrders).IsRequired();
            });
            builder.Entity<NeetOutMaterial>(n =>
            {
                n.ToTable("NeetOutMaterial");
                n.HasOne<MaterialBase>(c => c.Material).
                  WithMany(a => a.NeetOutMaterials).IsRequired(false).OnDelete(DeleteBehavior.SetNull);
                n.HasOne<TaskOrder>(c=>c.TheTaskOrder).
                WithMany(a=>a.neetOutMaterials).IsRequired(false).OnDelete(DeleteBehavior.SetNull); 

            });
            builder.Entity<Inlethouse>(n =>
            {
                n.ToTable("Inlethouse");
            });
            builder.Entity<InlethouseLine>(n =>
            {
                n.ToTable("InlethouseLine");
                n.HasOne<Inlethouse>(c => c.TheInlethouse).WithMany(a => a.inlethouselines).IsRequired();
                n.HasOne<Box>(o => o.Box).WithOne(d => d.InlethouseLine).HasForeignKey<Box>(o => o.Id_InlethouseLine);      
            });
            builder.Entity<TaskOrder>(n =>
            {
                n.ToTable("TaskOrder");
                n.HasOne<PutOutOrder>(c=>c.PutOutOrder).WithMany(a=>a.TaskOrders).IsRequired(false);
            });
            builder.Entity<lognote>(n =>
            {
                n.ToTable("lognote");
            });
            builder.Entity<MaterialProperty>(n =>
            {
                n.ToTable("MaterialProperty");
               
            });
            //view
            builder.Entity<View_Box>(n =>
            {
                n.ToView("View_Box").HasNoKey();
            });
            builder.Entity<View_Material>(n =>
            {
                n.ToView("View_Material").HasNoKey();
            });
            builder.Entity<View_TaskOrder>(n =>
            {
                n.ToView("View_TaskOrder").HasNoKey();
            });
            builder.Entity<View_Box_WarehouseLine>(n =>
            {
                n.ToView("View_Box_WarehouseLine").HasNoKey();
            });
            builder.Entity<View_Material_WarehouseLine>(n =>
            {
                n.ToView("View_Material_WarehouseLine").HasNoKey();
            });
            builder.Entity<View_NeetOutMaterial>(n =>
            {
                n.ToView("View_NeetOutMaterial").HasNoKey();
            });
            builder.Entity<View_WarehouseLine>(n =>
            {
                n.ToView("View_WarehouseLine").HasNoKey();
            });
        }


    
    }
  

}
